package dk.eclipse.android.helpers;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.ProgressBar;
import android.widget.Toast;

/**
 * This class is used to listen for SMS DELIVERED broadcasts.
 * 
 * @author Ronni Kahalani
 *
 */
public class SMSDeliveredBroadcastReciever extends BroadcastReceiver {
	private static final String MSG_SMS_DELIVERED_OK = "SMS for %s has been delivered";
	private static final String MSG_SMS_NOT_DELIVERED = "SMS was not delivered to: %s";

	private static final long[] VIBRATION = new long[] { 100, 250, 100, 250 };
	
	private int notificationId;
	private String phoneNumber;
	private String message;
	private ProgressBar progressBar;
	private int icon;

	
	
	/**
	 * Constructor.
	 * 
	 * @param notificationId
	 * @param icon
	 * @param phoneNumber
	 * @param message
	 * @param progressBar
	 */
	public SMSDeliveredBroadcastReciever(int notificationId, int icon, String phoneNumber, String message, ProgressBar progressBar) {
		super();
		this.notificationId = notificationId;
		this.icon = icon;
		this.phoneNumber = phoneNumber;
		this.message = message;
		this.progressBar = progressBar;
		
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		String msg = null;

		switch (getResultCode()) {
		case Activity.RESULT_OK:
			msg = MSG_SMS_DELIVERED_OK;
			break;
		case Activity.RESULT_CANCELED:
			msg = MSG_SMS_NOT_DELIVERED;
			break;
		}
		progressBar.setVisibility(ProgressBar.INVISIBLE);
		if (msg != null) {

			long when = System.currentTimeMillis();
			
			String notificationMsg = String.format(msg,
					new Object[] { phoneNumber});

			
			String deliveredMsg = String.format("The message was:\n%s ",
					new Object[] { message });

			NotificationHelper.sendNotification(context,
					notificationId, notificationMsg,
					Toast.LENGTH_SHORT, icon,
					notificationMsg, when,
					notificationMsg,
					deliveredMsg, VIBRATION);
			
		}
	}


}
